#! /bin/bash -
# auto setup ppptp on ubuntu
LC_ALL=C
# BEGIN Tool Functions
. $(dirname $0)/pffs
# END Tool Functions
setuppptp(){
	ifYesDo "catIntoFile $'nospoof on' /etc/host.conf"; # nospoof.;
	ifYesDo "apt-get -y update"; # update.;
	ifYesDo "apt-get -y install pptpd"; # intall pptpd.;
	ifYesDo "catIntoFile $'test	*	vpn4testonly	*' /etc/ppp/chap-secrets"; #add username and password to pptpd.;
	ifYesDo "sedIntoFile 'localip 10.0.0.234-238,10.0.0.245\nremoteip 10.0.1.134-238,10.0.1.245' /etc/pptpd.conf after '#remoteip 192.168.1.234-238,192.168.1.245'"; # set local ip address and remote ip address for pptpd.;
	ifYesDo "sedIntoFile 'ms-dns 8.8.8.8\nms-dns 208.67.220.220' /etc/ppp/pptpd-options after '#ms-dns 10.0.0.2'"; # add DNS server.;
	ifYesDo "sedIntoFile '## proxyarp' /etc/ppp/pptpd-options replace 'proxyarap'" "" "Comment the 'proxyrap' option" " Add an entry to this system's ARP [Address Resolution Protocol]\n table with the IP address of the peer and the Ethernet address of this\n system.  This will have the effect of making the peer appear to other\n systems to be on the local ethernet.\n (you do not need this if your PPTP server is responsible for routing\n packets to the clients -- James Cameron)\n ";
	ifYesDo "service pptpd restart"; # restart pptpd.;
	ifYesDo "sedIntoFile 'net.ipv4.ip_forward=1' /etc/sysctl.conf after '#net.ipv4.ip_forward=1' ; sysctl -p ;"; # enable net.ipv4.ip_forward.;
	ifYesDo "iptables --table nat --append POSTROUTING --out-interface ppp0 --jump MASQUERADE"; # create nat rule for iptables.;
	ifYesDo "iptables --table nat --append POSTROUTING --out-interface eth0 --jump MASQUERADE"; # create nat rule for iptables, if eth0 is exist, we may need this.;
	ifYesDo "iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu"; # solve a problem with MTU size variations.;
	ifYesDo "iptables -I FORWARD -i ppp+ -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j TCPMSS --set-mss 1356"; # solve the problem that the mtu of ppp is smaller than eth has.;
	#####################################################.;
	## save the rules and set them to restore on reboot .;
	ifYesDo "iptables-save > /etc/iptables-rules"; # save iptables rules.;
	ifYesDo "catIntoFile $'#!/bin/sh\n iptables-restore < /etc/iptables-rules\nexit 0' /etc/network/if-pre-up.d/iptablesload; chmod +x /etc/network/if-pre-up.d/iptablesload;"; # create /etc/network/if-pre-up.d/iptablesload and make it excutable.;
}
setuppptp;
